package sw6.visualschedule.adminpanel;

import sw6.visualschedule.R;
import sw6.visualschedule.entities.RepeatPattern;
import sw6.visualschedule.entities.ScheduleActivity;
import sw6.visualschedule.persistence.RepeatPatternRepo;
import sw6.visualschedule.persistence.ScheduleActivityRepo;
import tools.BundleHelper;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class RepeatPatternList extends ListActivity {
    private transient ScheduleActivityRepo mActivityService;
    private transient RepeatPatternRepo mRepeatPatternService;
    
    private transient int mActivityId;
    private transient ScheduleActivity mActivity;
    private transient List<RepeatPattern> mItems = new ArrayList<RepeatPattern>();
    private transient ArrayAdapter<RepeatPattern> mAdapter;
    
    @Override
    public void onCreate(final Bundle bundle) {
        super.onCreate(bundle);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
        mActivityService = new ScheduleActivityRepo(this);
        mRepeatPatternService = new RepeatPatternRepo(this);
        mActivityId = getIntent().getIntExtra(BundleHelper.ACTIVITY_ID, -1);
        
        mAdapter = new ArrayAdapter<RepeatPattern>(this, android.R.layout.simple_list_item_1, mItems);
        setListAdapter(mAdapter);
        
        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final AdapterView<?> adapter, final View view, final int pos, final long itemId) {
                return onLongListItemClick(view, pos, itemId);
            }
        });
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        
        mActivity = mActivityService.getById(mActivityId);
        setTitle(getString(R.string.adm_activityrepeatlist_title, mActivity.getTitle()));
       
        mItems = new ArrayList<RepeatPattern>(mActivity.getRepeatPatterns());
        mAdapter = new ArrayAdapter<RepeatPattern>(this, android.R.layout.simple_list_item_1, mItems);
        setListAdapter(mAdapter);
        
        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final AdapterView<?> adapter, final View view, final int pos, final long itemId) {
                return onLongListItemClick(view, pos, itemId);
            }
        });
        
        Toast.makeText(this, getString(R.string.repeatpatternlist_info), Toast.LENGTH_LONG).show();
    }
    
    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        final MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activityrepeatlist_pref, menu);
        
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        boolean result;
        
        if (item.getItemId() == R.id.add_repeat_pattern) {
            final Intent intent = new Intent().setClass(this, RepeatPatternConstructor.class);
            intent.putExtra(BundleHelper.ACTIVITY_ID, getIntent().getIntExtra(BundleHelper.ACTIVITY_ID, -1));
            startActivity(intent);
            result = true;
        } else {
            result = super.onOptionsItemSelected(item);
        }
        
        return result;
    }
    
    @Override
    protected void onListItemClick(final ListView list, final View view, final int position, final long itemId) {
        final RepeatPattern selected = mItems.get(position);
  
        final Intent intent = new Intent(this, RepeatPatternConstructor.class);
        
        intent.putExtra(BundleHelper.ACTIVITY_ID, mActivity.getId());
        intent.putExtra(BundleHelper.REPEAT_PATTERN_ID, selected.getId());
        startActivity(intent);
    };
    
    protected boolean onLongListItemClick(final View view, final int pos, final long itemId) {
        final RepeatPattern selected = mItems.get(pos);      
        
        new AlertDialog.Builder(this)
            .setTitle(R.string.dd_delete_repeatpattern)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int whichButton) {
                    deleteRepeatPattern(selected);
                }
             })
             .setNegativeButton(android.R.string.no, null)
             .setMessage(getString(R.string.dd_do_you_want_to_delete, selected.toString()))
             .show();
        return true;
    }
    
    private void deleteRepeatPattern(final RepeatPattern pattern) {
        mRepeatPatternService.delete(pattern);
        
        mAdapter.remove(pattern);
        mAdapter.notifyDataSetChanged();
    }
}
